/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import Entity.Complaint;
import Entity.ComplaintSend;
import Entity.InfoLogin;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;

/**
 *
 * @author h10t93
 */
public class EmpProcess {

    CallableStatement cs;
    ResultSet rs;
    DataConnection connection = new DataConnection();

    //login
    public boolean empLoin(String user, String pass) {
        MD5 md5 = new MD5();
        boolean result = false;
        String pss = md5.encryptMD5(pass);
        try {
            String sql = "SELECT * FROM tblEmployee WHERE tblEmployee.Employee_Username=? AND tblEmployee.Employee_Password=?";
            PreparedStatement ps = connection.getConnection().prepareCall(sql);
            ps.setString(1, user);
            ps.setString(2, pss);
            rs = ps.executeQuery();
            while (rs.next()) {
                result = true;
            }
            rs.close();
            ps.close();
            connection.disconnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    // get info user loin

    public Vector<InfoLogin> getInfoLoginUser(String user) {
        Vector<InfoLogin> ils = new Vector<InfoLogin>();
        try {
            CallableStatement cs = connection.getConnection().prepareCall("procInfoUserLoin ?");
            cs.setString(1, user);
            ResultSet rs = cs.executeQuery();
            while (rs.next()) {
                InfoLogin il = new InfoLogin();
                il.setId(rs.getInt(1));
                il.setName(rs.getString(2));
                ils.add(il);
            }
            rs.close();
            cs.close();
            connection.disconnection();
        } catch (Exception e) {
        }
        return ils;
    }
    // emp send complaints

    public boolean sendComp(ComplaintSend c) {
        int result = 0;
        try {
            cs = connection.getConnection().prepareCall("procEmpSendComp ?,?,?,?");
            cs.setInt(1, c.getEmpID());
            cs.setString(2, c.getTitle());
            cs.setInt(3, c.getCate());
            cs.setString(4, c.getContent());
            result = cs.executeUpdate();
            cs.close();
            connection.disconnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result > 0;
    }
    // view complaints has been send

    public Vector<Complaint> viewComp(int id) {
        Vector<Complaint> list = new Vector<Complaint>();
        try {
            cs = connection.getConnection().prepareCall("procViewCompSend ?");
            cs.setInt(1, id);
            rs = cs.executeQuery();
            while (rs.next()) {
                Complaint cmp = new Complaint();
                cmp.setComplaintID(rs.getInt(1));
                cmp.setTitle(rs.getString(2));
                cmp.setComplaintType(rs.getString(3));
                cmp.setCompContent(rs.getString(4));
                cmp.setDate(rs.getString(5));
                list.add(cmp);
            }
            rs.close();
            cs.close();
            connection.disconnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
    // view complaints has been send by status

    public Vector<Complaint> viewCompByStatus(int id, int status) {
        Vector<Complaint> list = new Vector<Complaint>();
        try {
            cs = connection.getConnection().prepareCall("procViewCompSendByStatus ? ,?");
            cs.setInt(1, id);
            cs.setInt(2, status);
            rs = cs.executeQuery();
            while (rs.next()) {
                Complaint cmp = new Complaint();
                cmp.setComplaintID(rs.getInt(1));
                cmp.setTitle(rs.getString(2));
                cmp.setComplaintType(rs.getString(3));
                cmp.setCompContent(rs.getString(4));
                cmp.setDate(rs.getString(5));
                list.add(cmp);
            }
            rs.close();
            cs.close();
            connection.disconnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

   
}
